﻿using AutoMapper;
using B.S.Outsourcing.Domain;
using B.S.Outsourcing.ErrorCode;
using B.S.Outsourcing.Infatructrue;
using B.S.Outsourcing.Writer.Api.Application.Command.OutSourOrderModel;
using MediatR;

namespace B.S.Outsourcing.Writer.Api.Application.CommandHandler.OutSourOrderModel
{
    public class DeleteOutsourOrderGetAllCommandHandler : IRequestHandler<DeleteOutsourOrderGetAllCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<OutsourcingOrderModel> baseRepository;
        private readonly DBHelper1 dbHelper;
        private readonly IMapper mapper;

        public DeleteOutsourOrderGetAllCommandHandler(IBaseRepository<OutsourcingOrderModel> baseRepository, DBHelper1 dbHelper, IMapper mapper)
        {
            this.baseRepository = baseRepository;
            this.dbHelper = dbHelper;
            this.mapper = mapper;
        }

        public async Task<ApiResult<int>> Handle(DeleteOutsourOrderGetAllCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            var sql = $"update OutsourcingOrderModel set IsDelete=1 where id in @id";
            var paramenters = new { id = request.Id.Distinct().ToArray() };
            var str = dbHelper.ExecuteNonQuerys(sql, paramenters);
            if (str>0)
            {
                result.Code = ApiEnums.成功;
                result.Message = "成功";
                result.Data = str;
            }
            else
            {
                result.Code = ApiEnums.失败;
                result.Message = "失败";
                result.Data = str;
            }
            return await Task.FromResult(result);
        }
    }
}
